//一般直接写在一个js文件中
layui.use(['element','form'],function () {
    var form = layui.form;
    //选择老人
    $("#elderlyName").on('click',function () {
        layer.open({
            type: 2,
            content: '/gbassessRecords/select/elderly',
            area: ['800px', '600px'],
            title:'选择老人',
            btn: ['确定 '],
            yes:function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
                var checkStatus = iframeWin.layui.table.checkStatus('mytable');
                if(checkStatus.data.length < 1 || checkStatus.data.length > 1){
                    iframeWin.layui.layer.msg("请选择一位老人");
                    return false;
                }
                var name = checkStatus.data[0].name;
                var id = checkStatus.data[0].id;
                $("#elderlyName").val(name);
                $("#elderlyId").val(id);
                $("#elderNo").val(checkStatus.data[0].idNo);
                //获取老人为付款的服务记录
                layer.close(index);
            }
        });
    });
    var buildingId;
    form.on('select(buildingId)', function(data){
        buildingId = data.value;
        $("#buildingNo").val($("#"+buildingId).text());
        if(data.value){
            $.ajax({
                url:"/room/floors",
                data:{buildingId:buildingId},
                dataType:'json',
                success:function(rs){
                    $("#floorNo").empty().append('<option value="">请选择楼层</option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#floorNo").append('<option value="'+ r +'">'+ r +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });

    form.on('select(floorNo)', function(data){
        var floor = data.value;
        if(data.value){
            $.ajax({
                url:"/bed/rooms",
                data:{buildingId:buildingId,floor:floor},
                dataType:'json',
                success:function(rs){
                    $("#roomId").empty().append('<option value=""></option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#roomId").append('<option value="'+ r.code +'">'+ r.code +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });
});